package com.hotel.service.impl;

import com.hotel.bean.Order;
import com.hotel.bean.Room;
import com.hotel.mapper.OrderMapper;
import com.hotel.mapper.RoomMapper;
import com.hotel.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertOidAndRid(Integer oid, Integer[] rids) {
        //删除原有的房间
        orderMapper.deleteOidAndRidByOid(oid);
        if(rids != null && rids.length>0){
            //添加
            orderMapper.insertOidAndRid(oid,rids);
            //讲订单修改为已预约
            orderMapper.updateOstateTo2(oid);
            Room room = new Room();
            //修改房间为已预定
            for (int i = 0; i < rids.length; i++) {
                room= roomMapper.selectByRid(rids[i]);
                roomMapper.updateRstate(room.getRnumber(),4);
            }
        }

    }
    @Override
    public List<Integer> selectRidByOid(Integer oid) {
        return orderMapper.selectRidByOid(oid);
    }
    @Override
    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }
    //完成订单删除表的联系，并且修改订单状态
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOstate(Integer oid) {
        orderMapper.deleteOidAndRidByOid(oid);
        orderMapper.updateOstate(oid);
    }

}
